<?php
// 本类由系统自动生成，仅供测试用途
namespace Admin\Controller;
use Think\Controller;
use Org\Util\Authentication;
class CarsController extends Controller {
    /**
     * 获取审核订单信息
     */
    public function getReviewInfo() {
      $data = I('post.');
      $cityid = D('Cars')->getCityData($data);
      if($cityid){
                $cityid["identify_url"]    = serializeMysql($cityid["identify_url"],1);
                $cityid["driver_url"]      = serializeMysql($cityid["driver_url"],1);
                $cityid["workproof_url"]   = serializeMysql($cityid["workproof_url"],1);
                $cityid["bankflow_url"]    = serializeMysql($cityid["bankflow_url"],1);
                $cityid["creditreport_url"]= serializeMysql($cityid["creditreport_url"],1);
                $cityid["otherdata_url"]= serializeMysql($cityid["otherdata_url"],1);
      }
      $this->ajaxReturn($cityid);
    }
    /**
     * 审核通过和拒绝状态修改
     */
    public function getStatus() {
      $data = I('post.');
      $order= M('Order');
      $operation  = M('order_operation');
      $arr =array(
          "usernum"=>session('lampzuo_usernum'),
          "orderid"=>$data['orderid'],
          "status" =>$data['status'],
          "remark" =>$data['remark'],
          "create_time" => time(),
        );
      $orderdata = array('status'=>$data['status'],'remark'=>"经理审核");
      $res  = $order->where(array('orderid'=>$data['orderid']))->save($orderdata);
      if($res){
        $retule = $operation->add($arr);
        if(!$retule){
          $this->ajaxReturn(array('ret'=>0,'msg'=>"记录操作失败"));
        }
        $this->ajaxReturn(array('ret'=>1,'msg'=>"操作成功"));
      }else{
        $this->ajaxReturn(array('ret'=>0,'msg'=>"操作失败"));
      }
    }

    /**
     * 查看详情
     */
    public function orderCarsXiangQing() {
      $data = I('post.');
      $cars = D('Cars');
      $res  = $cars->getCarsXiangQing($data);
      if($res){
        $this->ajaxReturn($res);
      }
    }
    /**
     * 创建订单
     * @return [type] [description]
     */

    public function addOrder()
    {
      $order= D('Order');
      $data = I('post.');
      $user = M('user');
      $res_user=$user->where(array('tel'=>$data['tel']))->find();
      if(!$res_user){
        echo "-2";die;
      }
      // $fileName="UserContactJson/".$data['tel'].'.json';
      // $result=ossGetObject($fileName);
      // if (!$result) {
      //   echo "-4";die;
      // }
      $data['uid']  = $res_user['id'];
      if($data['orderid']){
        $userarr =  $this->saveImage($data);
        $data['sfzArr1']  = array_filter($data['sfzArr1']);
        if(count($data['sfzArr1'])>0 && count($userarr['identify_url'])>0){
           $userarr['identify_url'] = array_merge($data['sfzArr1'],$userarr['identify_url']);
        }else{
           $userarr['identify_url'] = count($data['sfzArr1'])>0?$data['sfzArr1']:$userarr['identify_url'];
        }
        if(count($data['jszArr1'])>0 && count($userarr['driver_url'])>0){
           $userarr['driver_url']   = array_merge($data['jszArr1'],$userarr['driver_url']);
        }else{
            $userarr['driver_url']  = count($data['jszArr1'])>0?$data['jszArr1']:$userarr['driver_url'];
        }
        if(count($data['gzzmArr1'])>0 && count($userarr['workproof_url'])>0){
            $userarr['workproof_url']  = array_merge($data['gzzmArr1'],$userarr['workproof_url']);
        }else{
            $userarr['workproof_url']  = count($data['gzzmArr1'])>0?$data['gzzmArr1']:$userarr['workproof_url'];
        }
        if(count($data['yhlsArr1'])>0 && count($userarr['bankflow_url'])>0){
            $userarr['bankflow_url']   = array_merge($data['yhlsArr1'],$userarr['bankflow_url']);
        }else{
            $userarr['bankflow_url']   = count($data['yhlsArr1'])>0?$data['yhlsArr1']:$userarr['bankflow_url'];
        }
        if(count($data['zxArr1'])>0 && count($userarr['creditreport_url'])>0){
            $userarr['creditreport_url']   = array_merge($data['zxArr1'],$userarr['creditreport_url']);
        }else{
            $userarr['creditreport_url']   =count($data['zxArr1'])>0?$data['zxArr1']:$userarr['creditreport_url'];
        }
        if(count($data['buquanArr'])>0 && count($userarr['otherdata_url'])>0){
            $userarr['otherdata_url']   = array_merge($data['buquanArr'],$userarr['otherdata_url']);
        }else{
            $userarr['otherdata_url']   = count($data['buquanArr'])>0?$data['buquanArr']:$userarr['otherdata_url'];
        }
        $userarr['identify_url']     = serializeMysql($userarr['identify_url']);
        $userarr['driver_url']       = serializeMysql($userarr['driver_url']);
        $userarr['workproof_url']    = serializeMysql($userarr['workproof_url']);
        $userarr['bankflow_url']     = serializeMysql($userarr['bankflow_url']);
        $userarr['creditreport_url'] = serializeMysql($userarr['creditreport_url']);
        $userarr['otherdata_url']    = serializeMysql($userarr['otherdata_url']);
        $res = $order->getAddOrder($data,$userarr);
        if($res){
            echo "1";die;
        }else{
            echo "-1";die;       
        }
      }else{
            if(M('Order')->where(array('uid'=>$res_user['id']))->find()){
              echo "2";die;//判断该用户是否已有订单
            }
            $userarr =  $this->saveImage($data);

            $userarr['identify_url']     = serializeMysql($userarr['identify_url']);
            $userarr['driver_url']       = serializeMysql($userarr['driver_url']);
            $userarr['workproof_url']    = serializeMysql($userarr['workproof_url']);
            $userarr['bankflow_url']     = serializeMysql($userarr['bankflow_url']);
            $userarr['creditreport_url'] = serializeMysql($userarr['creditreport_url']);
            $res = $order->getAddOrder($data,$userarr);
          if($res){
              echo "1";die;
          }else{
              echo "-1";die;       
          }
      }
    }

    /**
     * 图片处理
     * $datepath = 'Public/Uploads/identify/'.$date;
        if(!ossGetObject($datepath))
        {
            //检查是否有该文件夹，如果没有就创建，并给予最高权限
            $res=createObjectDir($datepath);
        }
        ossPutObject($datepath.'/'.$identity_A_file,file_get_contents($path.$identity_A_file));
        ossPutObject($datepath.'/'.$identity_B_file,file_get_contents($path.$identity_B_file));
        ossPutObject($datepath.'/'.$people_H_file,file_get_contents($path.$people_H_file));
     */
    public function saveImage($data){
        $user = M('user');
        $userarr = array();
        $res_user=$user->where(array('tel'=>$data['tel']))->find();
        $new_file = "./Public/Image/Ceshi/".$res_user['id']."/";
        if(!file_exists($new_file)){
          $res = chmod($new_file, 0777);
        }
        if($data['sfz_zm']){
            foreach ($data['sfz_zm'] as $key => $value) {
              $newj = "./Public/Image/Ceshi/".$res_user['id']."/identify_url/";
              $datepath = "Public/Image/Ceshi/".$res_user['id']."/identify_url";
              if(!ossGetObject($datepath))
              {
                  //检查是否有该文件夹，如果没有就创建，并给予最高权限
                  $res=createObjectDir($datepath);
              }
              $result = Base64DecodeImage($value,$newj,createverifycode());
              rotate($newj.$result,intval($data['identify_A']));
              ImgThumb($newj.$result);
              ossPutObject($datepath.'/'.$result,file_get_contents(C('WEBURL').$newj.$result));
              $userarr['identify_url'][] = $newj.$result;
              @unlink(C('PUBLIC_ROUTE').$newj.$result);
           }
        }
        if($data['sfz_fm']){
            foreach ($data['sfz_fm'] as $key => $value) {
              $newj = "./Public/Image/Ceshi/".$res_user['id']."/identify_url/";
              $result = Base64DecodeImage($value,$newj,createverifycode());
              rotate($newj.$result,intval($data['identify_B']));
              ImgThumb($newj.$result);
              ossPutObject($datepath.'/'.$result,file_get_contents(C('WEBURL').$newj.$result));
              $userarr['identify_url'][] = $newj.$result;
              @unlink(C('PUBLIC_ROUTE').$newj.$result);
           }
        }
        if($data['sfz_bs']){
            foreach ($data['sfz_bs'] as $key => $value) {
              $newj = "./Public/Image/Ceshi/".$res_user['id']."/identify_url/";
              $result = Base64DecodeImage($value,$newj,createverifycode());
              rotate($newj.$result,intval($data['identify_H']));
              ImgThumb($newj.$result);
              ossPutObject($datepath.'/'.$result,file_get_contents(C('WEBURL').$newj.$result));
              $userarr['identify_url'][] = $newj.$result;
              @unlink(C('PUBLIC_ROUTE').$newj.$result);
           }
        }
        if($data['jsz']){
            if(count($data['jsz']) != 1){ 
              array_pop($data['jsz']); 
            }
            $datepath = "Public/Image/Ceshi/".$res_user['id']."/driver_url";
            if(!ossGetObject($datepath))
            {
                //检查是否有该文件夹，如果没有就创建，并给予最高权限
                $res=createObjectDir($datepath);
            }
            foreach ($data['jsz'] as $key => $value) {

                  $newj = "./Public/Image/Ceshi/".$res_user['id']."/driver_url/";
                  $result = Base64DecodeImage($value,$newj,createverifycode());
                  ImgThumb($newj.$result);
                  ossPutObject($datepath.'/'.$result,file_get_contents(C('WEBURL').$newj.$result));
                  $userarr['driver_url'][] = $newj.$result;
                  @unlink(C('PUBLIC_ROUTE').$newj.$result);
            }   
        }
        
        if($data['gzzm']){
            $datepath = "Public/Image/Ceshi/".$res_user['id']."/workproof_url";
            if(!ossGetObject($datepath))
            {
                //检查是否有该文件夹，如果没有就创建，并给予最高权限
                $res=createObjectDir($datepath);
            }
            foreach ($data['gzzm'] as $key => $value) {
                  $newj = "./Public/Image/Ceshi/".$res_user['id']."/workproof_url/";
                  $result = Base64DecodeImage($value,$newj,createverifycode());
                  ImgThumb($newj.$result);
                  ossPutObject($datepath.'/'.$result,file_get_contents(C('WEBURL').$newj.$result));
                  $userarr['workproof_url'][] = $newj.$result;
                  @unlink(C('PUBLIC_ROUTE').$newj.$result);
            }
        }
        if($data['yhls']){
            $datepath = "Public/Image/Ceshi/".$res_user['id']."/bankflow_url";
            if(!ossGetObject($datepath))
            {
                //检查是否有该文件夹，如果没有就创建，并给予最高权限
                $res=createObjectDir($datepath);
            }
            foreach ($data['yhls'] as $key => $value) {
                  $newj = "./Public/Image/Ceshi/".$res_user['id']."/bankflow_url/";
                  $result = Base64DecodeImage($value,$newj,createverifycode());
                  ImgThumb($newj.$result);
                  ossPutObject($datepath.'/'.$result,file_get_contents(C('WEBURL').$newj.$result));
                  $userarr['bankflow_url'][] = $newj.$result;
                  @unlink(C('PUBLIC_ROUTE').$newj.$result);
                
            }
        }
        if($data['zx']){
           $datepath = "Public/Image/Ceshi/".$res_user['id']."/creditreport_url";
           if(!ossGetObject($datepath))
           {
              //检查是否有该文件夹，如果没有就创建，并给予最高权限
              $res=createObjectDir($datepath);
           }
           foreach ($data['zx'] as $key => $value) {
                  $newj = "./Public/Image/Ceshi/".$res_user['id']."/creditreport_url/";
                  $result = Base64DecodeImage($value,$newj,createverifycode());
                  ImgThumb($newj.$result);
                  ossPutObject($datepath.'/'.$result,file_get_contents(C('WEBURL').$newj.$result));
                  $userarr['creditreport_url'][] = $newj.$result;
                  @unlink(C('PUBLIC_ROUTE').$newj.$result);
           }
        }
        if($data['qitadataArr']){
           $datepath = "Public/Image/Ceshi/".$res_user['id']."/otherdata_url";
           if(!ossGetObject($datepath))
           {
              //检查是否有该文件夹，如果没有就创建，并给予最高权限
              $res=createObjectDir($datepath);
           }
           foreach ($data['qitadataArr'] as $key => $value) {
                  $newj = "./Public/Image/Ceshi/".$res_user['id']."/otherdata_url/";
                  $result = Base64DecodeImage($value,$newj,createverifycode());
                  ImgThumb($newj.$result);
                  ossPutObject($datepath.'/'.$result,file_get_contents(C('WEBURL').$newj.$result));
                  $userarr['otherdata_url'][] = $newj.$result;
                  @unlink(C('PUBLIC_ROUTE').$newj.$result);
           }
        }
        return $userarr;
    }

    /**
      * 人脸识别
      */
    public function  getFace(){
      $data = I('post.');
      $userarr = array();
      $Credit  = D('Credit');
      $user    = M('User');
      // $res_user=$user->where(array('tel'=>$data['tel']))->find();
      $new_file = "./Public/Image/Face/";
      if(!file_exists($new_file)){
        $res=mkdir($new_file, 0777,true);
      }

      if($data['identify']){
          foreach ($data['identify'] as $key => $value) {
              if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $value, $result)){
                $type = $result[2];
                $newj_img = $new_file.time().createverifycode().'identify'.".{$type}";
                if (file_put_contents($newj_img, base64_decode(str_replace($result[1], '', $value)))){
                  $userarr['IdentityJust'] = C('PUBLIC_ROUTE').substr($newj_img,1);
                }
              }
         }
      }

      if($data['identifyback']){
          foreach ($data['identifyback'] as $key => $value) {
              if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $value, $result)){
                $type = $result[2];
                $newj_img = $new_file.time().createverifycode().'identifyback'.".{$type}";
                if (file_put_contents($newj_img, base64_decode(str_replace($result[1], '', $value)))){
                  $userarr['IdentityBack'] = C('PUBLIC_ROUTE').substr($newj_img,1);
                }
              }
         }
      }

      if($data['holdidentify']){
          foreach ($data['holdidentify'] as $key => $value) {
              if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $value, $result)){
                $type = $result[2];
                $newj_img = $new_file.time().createverifycode().'holdidentify'.".{$type}";
                if (file_put_contents($newj_img, base64_decode(str_replace($result[1], '', $value)))){
                  $userarr['UserFaceJust'] = C('PUBLIC_ROUTE').substr($newj_img,1);
                }
              }
         }
      }

      rotate($userarr['IdentityJust'],intval($data['identify_A']));
      rotate($userarr['IdentityBack'],intval($data['identify_B']));
      rotate($userarr['UserFaceJust'],intval($data['identify_H']));
      //$srcimg['IdentityJust'] = C('PUBLIC_ROUTE').substr($data['IdentityJust'],1);//身份证正面
      //$srcimg['IdentityBack'] = C('PUBLIC_ROUTE').substr($data['IdentityBack'],1);//身份证反面
      //$srcimg['UserFaceJust'] = C('PUBLIC_ROUTE').substr($data['UserFaceJust'],1);//身份人脸照片
      $arr    = array();
      $auth   = new Authentication();
      $res    = $auth->JdcAuth($userarr);
      if($res['error']){
           $arr['error']   = $res['error'];
           $arr['message'] = $res['message'];
           $this->ajaxReturn($arr);
      }
      $where  = "1=1";
      if($data['id']){
        $where  .= " and id  <> (".$data['id'].")";
      }
      $where    .= " and identify = ("."'".$res['data']['IdentityJustInfo']['id_card_number']."'".")";
      $resfalse  = $user->where($where)->find();
      if($resfalse){
          $arr['message'] = "该用户已在平台办理过订单,系统将自动取消订单";
          $arr['error']   = "couerror";
          $this->ajaxReturn($arr);
      }
      $arr['address']  = $res['data']['IdentityJustInfo']['address'];//户籍地址
      $arr['sex']      = $res['data']['IdentityJustInfo']['gender'];//性别
      $arr['birthday'] = $res['data']['IdentityJustInfo']['birthday']['year'].'-'.$res['data']['IdentityJustInfo']['birthday']['month'].'-'.$res['data']['IdentityJustInfo']['birthday']['day'];//生日
      $arr['fullanme'] = $res['data']['IdentityJustInfo']['name'];
      $arr['minority'] = $res['data']['IdentityJustInfo']['race'];
      $arr['identify'] = $res['data']['IdentityJustInfo']['id_card_number'];
      $arr['policeoffice'] = $res['data']['IdentityBackInfo']['issued_by'];
      $arr['message'] = $res['message'];
      $arr['success'] = $res['success'];
      $arr['error']   = $res['error'];
      @unlink($userarr['IdentityJust']);
      @unlink($userarr['IdentityBack']);
      @unlink($userarr['UserFaceJust']);
      $this->ajaxReturn($arr);
    }
}